package org.com.fw.web.security;

import javax.annotation.Resource;

import org.com.mars.dao.system.SysUserDao;
import org.com.mars.pojo.system.SysUser;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 通过UserDetailsService拿到用户信息后，authenticationManager对比用户的密码（即验证用户），
 * 然后这个AuthenticationProcessingFilter拦截器就过了。
 * 
 * @author BIG
 *
 */
@Service
public class MarsUserDetailsService implements UserDetailsService {

	@Resource
	private SysUserDao sysUserDao;

	/**
	 * 登陆验证时，通过username获取用户的所有权限信息，
	 * 并返回User放到spring的全局缓存SecurityContextHolder中，以供授权器使用
	 */
	@Override
	public UserDetails loadUserByUsername(String useraccount) throws UsernameNotFoundException {

		// 通过用户名取得用户的信息
		SysUser sysUser = getSysUserByUserAccount(useraccount);
		if (sysUser == null) {
			throw new UsernameNotFoundException("用户不存在!");
		}
		return sysUser;
	}

	/**
	 * 通过用户名取得用户的信息.
	 * 
	 * @param username
	 * @return
	 */
	public SysUser getSysUserByUserAccount(String useraccount) {
		SysUser user = sysUserDao.getSysUserByUserAccount(useraccount);
		return user;
	}

}
